package com.tiancikeji.zaoke.app.dao.food.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.tiancikeji.zaoke.app.dao.food.FoodDao;
import com.tiancikeji.zaoke.app.model.food.Food;

public class FoodDaoImpl extends JdbcDaoSupport implements FoodDao{

	private static final String TABLE = "`zaoke`.`food`";
	private static final String QUERY_VIEW = "`id`, `name`, `type`, `sale_price`, `desc`, `state`, `sequence`, `add_userid`, `add_time`, `lastmod_userid`,`lastmod_time`";

	public Set<Food> getFoodsByState(Integer[] states){
		String sql = "select " + QUERY_VIEW + " from " + TABLE + " where `state` in (";
		sql += StringUtils.join(states, ",");
		sql += ")";
		
		Set<Food> result = new HashSet<Food>();
		List<Food> foods = getJdbcTemplate().query(sql, new RowMapper<Food>() {

			public Food mapRow(ResultSet rs, int rowNum) throws SQLException {
				Food food = new Food();
				food.setId(rs.getLong("id"));
				food.setName(rs.getString("name"));
				food.setType(rs.getInt("type"));
				food.setSalePrice(rs.getDouble("sale_price"));
				food.setDesc(rs.getString("desc"));
				food.setState(rs.getInt("state"));
				food.setSequence(rs.getInt("sequence"));
				food.setAddUserid(rs.getLong("add_userid"));
				food.setAddTime(rs.getDate("add_time"));
				food.setLastmodUserid(rs.getLong("lastmod_userid"));
				food.setLastmodTime(rs.getDate("lastmod_time"));
				return food;
			}
		});
		
		if(foods != null && foods.size() > 0)
			result.addAll(foods);
		
		return result;
	}
}
